package com.bawei.sx1_news.model.base

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.bawei.sx1_news.R
import java.lang.reflect.ParameterizedType

abstract class BaseActivity<V:ViewModel,D:ViewDataBinding>(private var layout:Int) : AppCompatActivity() {

    lateinit var viewModel: V
    lateinit var binding: D

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding=DataBindingUtil.setContentView(this,layout)
        binding.lifecycleOwner=this

        var type=javaClass.genericSuperclass as ParameterizedType
        var clazz=type.actualTypeArguments[0] as Class<V>

        viewModel=ViewModelProvider(this).get(clazz)

        initView()
        initData()
    }

    abstract fun initData()

    abstract fun initView()

    override fun onDestroy() {
        super.onDestroy()
        if (binding!=null){
            binding.unbind()
        }
    }
}